package com.imooc.controller;

import com.imooc.pojo.Users;
import com.imooc.pojo.vo.UserVO;
import com.imooc.resourse.FileResource;
import com.imooc.service.FdfsService;
import com.imooc.service.center.CenterUserService;
import com.imooc.utils.CookieUtils;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: 用户中心的用户前端控制器
 * @author: Yz
 * @create: 2020-07-09 14:29
 **/
@RestController
@RequestMapping("/fdfs")
public class CenterUserController extends BaseController {

    @Autowired
    private FdfsService fdfsService;

    @Autowired
    private FileResource fileResource;

    @Autowired
    private CenterUserService centerUserService;

    @PostMapping("/uploadFace")
    public IMOOCJSONResult uploadFace(
            @RequestParam("userId") String userId, MultipartFile file,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        String path = "";
        //开始文件上传
        if (file != null) {
            //获得文件上传的名称
            String filename = file.getOriginalFilename();
            if (StringUtils.isNotBlank(filename)) {
                //文件重命名 imooc-face.png ->["imooc-face","png"]
                String[] fileNameArray = filename.split("\\.");
                //获取文件的后缀名
                String suffix = fileNameArray[fileNameArray.length - 1];
                //验证图片格式
                if (!suffix.equalsIgnoreCase("png") &&
                        !suffix.equalsIgnoreCase("jpg") &&
                        !suffix.equalsIgnoreCase("jpeg")) {
                    return IMOOCJSONResult.errorMsg("图片格式不正确");
                }
                //path = fdfsService.upload(file, suffix);
                path = fdfsService.uploadOSS(file,userId,suffix);
                System.out.println(path);
            }
        } else {
            return IMOOCJSONResult.errorMsg("文件不能为空");
        }
        if (StringUtils.isNotBlank(path)){
            //由于浏览器可能存在缓存的情况，所以在这里，我们需要加上时间戳来保证更新后的图片可以及时刷新
            //String finalUserFaceUrl = fileResource.getHost() + path;
            String finalUserFaceUrl = fileResource.getOssHost() + path;
            //更新用户头像给数据库
            Users userResult = centerUserService.updateUserFace(userId, finalUserFaceUrl);
            //userResult = setNullProperty(userResult);
            //增加令牌token，会整合redis，分布式会话
            UserVO userVO = convertUserVO(userResult);
            CookieUtils.setCookie(request, response, "user",
                    JsonUtils.objectToJson(userVO), true);
        }else {
            return IMOOCJSONResult.errorMsg("上传头像失败");
        }


        return IMOOCJSONResult.ok();
    }


}
